package com.KKd.config;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author 14587
 * @date 2020/11/7 16:38
 */
public class LoginInterceptor implements HandlerInterceptor {
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        //我们需要判断什么时候登录
        //如果登录的话，放行
        //1.session里面有用户信息，说明登录了
        if (session.getAttribute("userInfo")!=null){
            return true;
        }
        //2.去登录页面，可以直接放行
        if (request.getRequestURI().contains("goLogin")){
            return true;
        }
        //3.在登录页面登录，可以直接放行
        if (request.getRequestURI().contains("login")){
            return true;
        }
        //没登陆的话，跳转到其他页面
        request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request,response);
        return false;
    }
}
